//
//  ZQTopicCell.m
//  BuDeJie
//
//  Created by 仲琦 on 16/4/13.
//  Copyright © 2016年 仲琦. All rights reserved.
//

#import "ZQTopicCell.h"
#import "ZQTopic.h"


#import "ZQTopicPictureView.h"
#import "ZQTopicVideoView.h"
#import "ZQTopicVoiceView.h"

#import <UIImageView+WebCache.h>
@interface ZQTopicCell ()

// 控件的命名 = 功能作用 + 控件类型
@property (weak, nonatomic) IBOutlet UIImageView *profileImageView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *passtimeLabel;
@property (weak, nonatomic) IBOutlet UILabel *text_label;
@property (weak, nonatomic) IBOutlet UIButton *dingButton;
@property (weak, nonatomic) IBOutlet UIButton *caiButton;
@property (weak, nonatomic) IBOutlet UIButton *repostButton;
@property (weak, nonatomic) IBOutlet UIButton *commentButton;
@property (weak, nonatomic) IBOutlet UIView *topCmtView;
@property (weak, nonatomic) IBOutlet UILabel *topCmtLabel;


/** 图片控件 */
@property (nonatomic, weak)ZQTopicPictureView *picture;
/** 声音控件 */
@property (nonatomic, weak)ZQTopicVoiceView *voice;
/** 视频控件 */
@property (nonatomic, weak)ZQTopicVideoView *video;
@end

@implementation ZQTopicCell

#pragma mark - 懒加载
/**
 *  懒加载cell的控件
 */
- (ZQTopicPictureView *)picture
{
    if (_picture == nil ) {
        ZQTopicPictureView *picture = [ZQTopicPictureView zq_viewFromXib];
        [self.contentView addSubview:picture];
        _picture = picture;
    }
    return _picture;
}

- (ZQTopicVideoView *)video
{
    if (_video == nil) {
        ZQTopicVideoView *video = [ZQTopicVideoView zq_viewFromXib];
        [self.contentView addSubview:video];
        _video = video ;
    }
    return _video;
}

- (ZQTopicVoiceView *)voice
{
    if (_voice == nil) {
        ZQTopicVoiceView *voice = [ZQTopicVoiceView zq_viewFromXib];
        [self.contentView addSubview:voice];
        _voice = voice;
    }
    return _voice;
}



- (void)layoutSubviews
{
    [super layoutSubviews];
    
    switch (self.topic.type) {
        case ZQTopicTypePicture :
            self.picture.frame = self.topic.minFrame;
            break;
        case ZQTopicTypeVideo :
            self.video.frame = self.topic.minFrame;
            break;
        case ZQTopicTypeVoice:
            self.voice.frame = self.topic.minFrame;
            break;
        default:
            break;
    }
    
}
/**
 *  加载xib文件后调用
 */
- (void)awakeFromNib {
    
    //tableView设置背景图片,没有 图片属性,  有背景view
    self.backgroundView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"mainCellBackground"]];
    
    //设置选中cell的属性
//    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
}

-(void)setFrame:(CGRect)frame
{
    
    frame.size.height -= ZQMargin;

    [super setFrame:frame];

}

/**
 *  从写模型的set方法 , 加载cell中的内容
 */
-(void)setTopic:(ZQTopic *)topic
{
    _topic = topic;
    
    [self.profileImageView sd_setImageWithURL:[NSURL URLWithString:topic.profile_image] placeholderImage:[UIImage imageNamed:@"defaultUserIcon"]];
    
    self.nameLabel.text = topic.name;
    self.passtimeLabel.text = topic.passtime;
    self.text_label.text = topic.text;
    
    [self setupButton:self.dingButton number:topic.ding placeholder:@"顶"];
    [self setupButton:self.caiButton number:topic.cai placeholder:@"踩"];
    [self setupButton:self.repostButton number:topic.repost placeholder:@"分享"];
    [self setupButton:self.commentButton number:topic.comment placeholder:@"评论"];
    

    
    
    //判断是否有热门评论
    if (topic.top_cmt.count) {
        //取消隐藏
        self.topCmtView.hidden = NO;
        //取出模型中的数据
        NSString *username = topic.top_cmt.firstObject[@"user"][@"username"];
        NSString *content = topic.top_cmt.firstObject[@"content"];
        
        //输入数据
        self.topCmtLabel.text = [NSString stringWithFormat:@" %@ : %@", username, content];
        
        //判断热门评论是否有内容, 没有内容就说明是语音数据
        if (content.length == 0) {
            self.topCmtLabel.text = @"语音数据";
        }
        
        //没有热门评论隐藏view
    }else{
        self.topCmtView.hidden = YES;
    }
    
    /* 懒加载全部控件,判断cell是什么属性,隐藏其他的控件*/
    switch (topic.type) {
        case ZQTopicTypePicture :{
            
            self.picture.hidden = NO;
            self.voice.hidden = YES;
            self.video.hidden = YES;
            self.picture.topic = topic;
            break;
            
        }
        case ZQTopicTypeVideo:{
            
            self.picture.hidden = YES;
            self.voice.hidden = YES;
            self.video.hidden = NO;
            self.video.topic = topic;
            
            break;
            
        }
        case ZQTopicTypeVoice:{
            
            self.picture.hidden = YES;
            self.voice.hidden = NO;
            self.video.hidden = YES;
            self.voice.topic = topic;
            
            break;
            
        }
        case ZQTopicTypeWord:{
            
            self.picture.hidden =YES;
            self.voice.hidden = YES;
            self.video.hidden = YES;
            
            break;
        }
            
        default:
            break;
    }
    
    
}



- (void)setupButton:(UIButton *)btn number:(NSInteger )number placeholder:(NSString *)placeholder
{
    if (number >= 10000) {
        [btn setTitle:[NSString stringWithFormat:@"%.1f万", number /10000.0] forState:UIControlStateNormal];
    }else if (number > 0){
        [btn setTitle:[NSString stringWithFormat:@"%zd", number] forState:UIControlStateNormal];
    }else{
        [btn setTitle:[NSString stringWithFormat:@"%@", placeholder] forState:UIControlStateNormal];
    }
    
}

@end
